{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>tpep_pickup_datetime</th>\n",
       "      <th>passenger_count</th>\n",
       "      <th>trip_distance</th>\n",
       "      <th>fare_amount</th>\n",
       "      <th>extra</th>\n",
       "      <th>mta_tax</th>\n",
       "      <th>tip_amount</th>\n",
       "      <th>tolls_amount</th>\n",
       "      <th>improvement_surcharge</th>\n",
       "      <th>total_amount</th>\n",
       "      <th>congestion_surcharge</th>\n",
       "      <th>pre_tip_amount</th>\n",
       "      <th>tip_percentage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019-01-01 00:46:40</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1.5</td>\n",
       "      <td>7.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1.65</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>9.95</td>\n",
       "      <td>NaN</td>\n",
       "      <td>8.3</td>\n",
       "      <td>0.198795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019-01-01 00:59:47</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.6</td>\n",
       "      <td>14.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>1.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>16.30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15.3</td>\n",
       "      <td>0.065359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2018-12-21 13:48:30</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>5.80</td>\n",
       "      <td>NaN</td>\n",
       "      <td>5.8</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2018-11-28 15:52:25</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>7.55</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.8</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2018-11-28 15:56:57</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.5</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>55.55</td>\n",
       "      <td>NaN</td>\n",
       "      <td>52.8</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  tpep_pickup_datetime  passenger_count  trip_distance  fare_amount  extra  \\\n",
       "0  2019-01-01 00:46:40              1.0            1.5          7.0    0.5   \n",
       "1  2019-01-01 00:59:47              1.0            2.6         14.0    0.5   \n",
       "2  2018-12-21 13:48:30              3.0            0.0          4.5    0.5   \n",
       "3  2018-11-28 15:52:25              5.0            0.0          3.5    0.5   \n",
       "4  2018-11-28 15:56:57              5.0            0.0         52.0    0.0   \n",
       "\n",
       "   mta_tax  tip_amount  tolls_amount  improvement_surcharge  total_amount  \\\n",
       "0      0.5        1.65           0.0                    0.3          9.95   \n",
       "1      0.5        1.00           0.0                    0.3         16.30   \n",
       "2      0.5        0.00           0.0                    0.3          5.80   \n",
       "3      0.5        0.00           0.0                    0.3          7.55   \n",
       "4      0.5        0.00           0.0                    0.3         55.55   \n",
       "\n",
       "   congestion_surcharge  pre_tip_amount  tip_percentage  \n",
       "0                   NaN             8.3        0.198795  \n",
       "1                   NaN            15.3        0.065359  \n",
       "2                   NaN             5.8        0.000000  \n",
       "3                   NaN             4.8        0.000000  \n",
       "4                   NaN            52.8        0.000000  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "filenames = ['../data/nyc_taxi_2019-01.csv', '../data/nyc_taxi_2019-07.csv']\n",
    "\n",
    "all_dfs = [pd.read_csv(one_filename, \n",
    "           usecols=['tpep_pickup_datetime', 'passenger_count', 'trip_distance',\n",
    "                    'fare_amount','extra','mta_tax','tip_amount','tolls_amount',\n",
    "                    'improvement_surcharge','total_amount','congestion_surcharge'],\n",
    "           parse_dates=['tpep_pickup_datetime'])\n",
    "           for one_filename in filenames]\n",
    "\n",
    "df = pd.concat(all_dfs)\n",
    "\n",
    "df['pre_tip_amount'] = df[['fare_amount', 'extra', 'mta_tax', 'tolls_amount', 'improvement_surcharge', 'congestion_surcharge']].sum(axis='columns')\n",
    "df['tip_percentage'] = df['tip_amount'] / df['pre_tip_amount']\n",
    "\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "We saw that 32 percent of riders don't tip at all. Of those who *do*, what percentage do they tip, on average?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.19146519965282618"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[df['tip_amount'] >0, 'tip_percentage'].mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "How many of the rides in our data set, supposedly from January and July 2019, are from outside of those dates?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(816, 13)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[(df['tpep_pickup_datetime'] < '2019-01-01 00:00:00') |\n",
    "   ((df['tpep_pickup_datetime'] > '2019-01-31 23:59:59') & (df['tpep_pickup_datetime'] < '2019-07-01 00:00:00')) |\n",
    "   (df['tpep_pickup_datetime'] > '2019-07-31 23:59:59')].shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "Looking only at dates in January and July, on what week did passengers tip the greatest percentage?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = df.set_index('tpep_pickup_datetime')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tpep_pickup_datetime\n",
       "2019-02-03    0.141979\n",
       "2019-01-27    0.138930\n",
       "2019-01-20    0.138536\n",
       "2019-01-13    0.137901\n",
       "2019-01-06    0.126983\n",
       "2019-08-04    0.124910\n",
       "2019-07-14    0.123459\n",
       "2019-07-21    0.123341\n",
       "2019-07-28    0.123036\n",
       "2019-07-07    0.112952\n",
       "Name: tip_percentage, dtype: float64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.concat([df['2019-01-01':'2019-01-31'],\n",
    "           df['2019-07-01':'2019-07-31']])\n",
    "\n",
    "df.resample('1W')['tip_percentage'].mean().sort_values(ascending=False).dropna()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
